﻿namespace ACCOUNTING
{
    using ACCOUNTING.LVY.Business;
    using ACCOUNTING.My.Resources;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.Sql;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Drawing;
    using System.IO;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    [DesignerGenerated]
    public class Frm_TaoDuLieuMoi : Form
    {
        private static List<WeakReference> __ENCList = new List<WeakReference>();
        [AccessedThroughProperty("CboTenServer")]
        private ComboBox _CboTenServer;
        [AccessedThroughProperty("CmdThoat")]
        private Button _CmdThoat;
        [AccessedThroughProperty("CmdThucHien")]
        private Button _CmdThucHien;
        [AccessedThroughProperty("cmdTim")]
        private Button _cmdTim;
        [AccessedThroughProperty("FolderBrowserDialog1")]
        private FolderBrowserDialog _FolderBrowserDialog1;
        [AccessedThroughProperty("GroupBox1")]
        private GroupBox _GroupBox1;
        [AccessedThroughProperty("Label1")]
        private Label _Label1;
        [AccessedThroughProperty("Label2")]
        private Label _Label2;
        [AccessedThroughProperty("Label3")]
        private Label _Label3;
        [AccessedThroughProperty("Label4")]
        private Label _Label4;
        [AccessedThroughProperty("Label5")]
        private Label _Label5;
        [AccessedThroughProperty("TxtPass")]
        private TextBox _TxtPass;
        [AccessedThroughProperty("TxtTenCSDL")]
        private TextBox _TxtTenCSDL;
        [AccessedThroughProperty("txtThuMuc")]
        private TextBox _txtThuMuc;
        [AccessedThroughProperty("TxtUserName")]
        private TextBox _TxtUserName;
        private IContainer components;
        public int pLoai;

        [DebuggerNonUserCode]
        public Frm_TaoDuLieuMoi()
        {
            base.Load += new EventHandler(this.Frm_TaoDuLieuMoi_Load);
            List<WeakReference> list = __ENCList;
            lock (list)
            {
                __ENCList.Add(new WeakReference(this));
            }
            this.InitializeComponent();
        }

        public bool CheckThongTinCongTy()
        {
            bool flag=false;
            string commandText = "SELECT * FROM HT_Company";
            try
            {
                flag = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0].Rows.Count > 0;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool CheckValid()
        {
            if (this.CboTenServer.Text.Trim() == "")
            {
                LVYModule.ShowWarning("Bạn phải chọn t\x00ean Server");
                return false;
            }
            if (this.TxtUserName.Text.Trim() == "")
            {
                LVYModule.ShowWarning("Bạn phải nh\x00e2p Username");
                return false;
            }
            if (this.TxtPass.Text.Trim() == "")
            {
                LVYModule.ShowWarning("Bạn phải nh\x00e2p Password");
                return false;
            }
            if (this.TxtTenCSDL.Text.Trim() == "")
            {
                LVYModule.ShowWarning("Bạn phải nh\x00e2p t\x00ean cơ sở dữ liệu cần tạo");
                return false;
            }
            if (this.txtThuMuc.Text.Trim() == "")
            {
                LVYModule.ShowWarning("Bạn phải chọn thư mục lưu trữ cơ sở dữ liệu");
                return false;
            }
            return true;
        }

        private void CmdThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void CmdThucHien_Click(object sender, EventArgs e)
        {
            if (this.CheckValid())
            {
                try
                {
                    this.RestoreDatabase();
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    Exception exception = exception1;
                    MessageBox.Show(exception.Message);
                    ProjectData.ClearProjectError();
                }
            }
        }

        private void cmdTim_Click(object sender, EventArgs e)
        {
            if (this.FolderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtThuMuc.Text = this.FolderBrowserDialog1.SelectedPath;
            }
        }

        [DebuggerNonUserCode]
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing && (this.components != null))
                {
                    this.components.Dispose();
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }

        private void Frm_TaoDuLieuMoi_Load(object sender, EventArgs e)
        {
            this.LoadServer();
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
            this.TxtPass = new TextBox();
            this.GroupBox1 = new GroupBox();
            this.cmdTim = new Button();
            this.TxtUserName = new TextBox();
            this.Label5 = new Label();
            this.Label4 = new Label();
            this.CboTenServer = new ComboBox();
            this.Label3 = new Label();
            this.txtThuMuc = new TextBox();
            this.Label2 = new Label();
            this.TxtTenCSDL = new TextBox();
            this.Label1 = new Label();
            this.CmdThucHien = new Button();
            this.CmdThoat = new Button();
            this.FolderBrowserDialog1 = new FolderBrowserDialog();
            this.GroupBox1.SuspendLayout();
            this.SuspendLayout();
            Point point = new Point(120, 0x47);
            this.TxtPass.Location = point;
            this.TxtPass.Name = "TxtPass";
            Size size = new Size(0x8a, 20);
            this.TxtPass.Size = size;
            this.TxtPass.TabIndex = 4;
            this.TxtPass.UseSystemPasswordChar = true;
            this.GroupBox1.BackColor = Color.Transparent;
            this.GroupBox1.Controls.Add(this.cmdTim);
            this.GroupBox1.Controls.Add(this.TxtPass);
            this.GroupBox1.Controls.Add(this.TxtUserName);
            this.GroupBox1.Controls.Add(this.Label5);
            this.GroupBox1.Controls.Add(this.Label4);
            this.GroupBox1.Controls.Add(this.CboTenServer);
            this.GroupBox1.Controls.Add(this.Label3);
            this.GroupBox1.Controls.Add(this.txtThuMuc);
            this.GroupBox1.Controls.Add(this.Label2);
            this.GroupBox1.Controls.Add(this.TxtTenCSDL);
            this.GroupBox1.Controls.Add(this.Label1);
            point = new Point(2, 9);
            this.GroupBox1.Location = point;
            this.GroupBox1.Name = "GroupBox1";
            size = new Size(450, 0x9a);
            this.GroupBox1.Size = size;
            this.GroupBox1.TabIndex = 0;
            this.GroupBox1.TabStop = false;
            this.GroupBox1.Text = "Tạo dữ liệu mới";
            point = new Point(410, 0x7b);
            this.cmdTim.Location = point;
            this.cmdTim.Name = "cmdTim";
            size = new Size(30, 0x17);
            this.cmdTim.Size = size;
            this.cmdTim.TabIndex = 7;
            this.cmdTim.Text = ". . .";
            this.cmdTim.UseVisualStyleBackColor = true;
            point = new Point(120, 0x2b);
            this.TxtUserName.Location = point;
            this.TxtUserName.Name = "TxtUserName";
            size = new Size(0x8a, 20);
            this.TxtUserName.Size = size;
            this.TxtUserName.TabIndex = 3;
            this.Label5.AutoSize = true;
            point = new Point(9, 0x49);
            this.Label5.Location = point;
            this.Label5.Name = "Label5";
            size = new Size(0x35, 13);
            this.Label5.Size = size;
            this.Label5.TabIndex = 6;
            this.Label5.Text = "Password";
            this.Label4.AutoSize = true;
            point = new Point(9, 0x2f);
            this.Label4.Location = point;
            this.Label4.Name = "Label4";
            size = new Size(0x37, 13);
            this.Label4.Size = size;
            this.Label4.TabIndex = 4;
            this.Label4.Text = "Username";
            this.CboTenServer.FormattingEnabled = true;
            point = new Point(0x79, 0x10);
            this.CboTenServer.Location = point;
            this.CboTenServer.Name = "CboTenServer";
            size = new Size(320, 0x15);
            this.CboTenServer.Size = size;
            this.CboTenServer.TabIndex = 2;
            this.Label3.AutoSize = true;
            point = new Point(8, 0x17);
            this.Label3.Location = point;
            this.Label3.Name = "Label3";
            size = new Size(0x3a, 13);
            this.Label3.Size = size;
            this.Label3.TabIndex = 2;
            this.Label3.Text = "T\x00ean server";
            point = new Point(0x79, 0x7c);
            this.txtThuMuc.Location = point;
            this.txtThuMuc.Name = "txtThuMuc";
            this.txtThuMuc.ReadOnly = true;
            size = new Size(0x11c, 20);
            this.txtThuMuc.Size = size;
            this.txtThuMuc.TabIndex = 6;
            this.txtThuMuc.Text = @"D:\";
            this.Label2.AutoSize = true;
            point = new Point(8, 0x80);
            this.Label2.Location = point;
            this.Label2.Name = "Label2";
            size = new Size(0x61, 13);
            this.Label2.Size = size;
            this.Label2.TabIndex = 10;
            this.Label2.Text = "Thư mục lưu CSDL";
            point = new Point(120, 0x62);
            this.TxtTenCSDL.Location = point;
            this.TxtTenCSDL.Name = "TxtTenCSDL";
            size = new Size(0x142, 20);
            this.TxtTenCSDL.Size = size;
            this.TxtTenCSDL.TabIndex = 5;
            this.Label1.AutoSize = true;
            point = new Point(7, 0x66);
            this.Label1.Location = point;
            this.Label1.Name = "Label1";
            size = new Size(0x6c, 13);
            this.Label1.Size = size;
            this.Label1.TabIndex = 8;
            this.Label1.Text = "T\x00ean cơ sở dữ liệu mới";
            this.CmdThucHien.Image = ACCOUNTING.My.Resources.Resources.Selected;
            this.CmdThucHien.ImageAlign = ContentAlignment.MiddleLeft;
            point = new Point(0x110, 180);
            this.CmdThucHien.Location = point;
            this.CmdThucHien.Name = "CmdThucHien";
            size = new Size(0x5d, 0x17);
            this.CmdThucHien.Size = size;
            this.CmdThucHien.TabIndex = 1;
            this.CmdThucHien.Text = "Thực hiện";
            this.CmdThucHien.UseVisualStyleBackColor = true;
            this.CmdThoat.Image = ACCOUNTING.My.Resources.Resources.Closed;
            this.CmdThoat.ImageAlign = ContentAlignment.MiddleLeft;
            point = new Point(0x175, 180);
            this.CmdThoat.Location = point;
            this.CmdThoat.Name = "CmdThoat";
            size = new Size(0x4f, 0x17);
            this.CmdThoat.Size = size;
            this.CmdThoat.TabIndex = 2;
            this.CmdThoat.Text = "Tho\x00e1t";
            this.CmdThoat.UseVisualStyleBackColor = true;
            SizeF ef = new SizeF(6f, 13f);
            this.AutoScaleDimensions = ef;
            this.AutoScaleMode = AutoScaleMode.Font;
            this.BackgroundImage = ACCOUNTING.My.Resources.Resources.background;
            size = new Size(0x1c9, 0xd8);
            this.ClientSize = size;
            this.Controls.Add(this.GroupBox1);
            this.Controls.Add(this.CmdThucHien);
            this.Controls.Add(this.CmdThoat);
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Frm_TaoDuLieuMoi";
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "TAO DU LIEU MOI";
            this.GroupBox1.ResumeLayout(false);
            this.GroupBox1.PerformLayout();
            this.ResumeLayout(false);
        }

        public void LoadServer()
        {
            if (this.CboTenServer.Items.Count == 0)
            {
                 IEnumerator enumerator=null;
                Cursor.Current = Cursors.WaitCursor;
                DataTable dataSources = SqlDataSourceEnumerator.Instance.GetDataSources();
                try
                {
                    enumerator = dataSources.Rows.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        DataRow current = (DataRow) enumerator.Current;
                        if (current["InstanceName"].ToString() == "")
                        {
                            this.CboTenServer.Items.Add(RuntimeHelpers.GetObjectValue(current["ServerName"]));
                        }
                        else
                        {
                            this.CboTenServer.Items.Add(current["ServerName"].ToString() + @"\" + current["InstanceName"].ToString());
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
        }

        public void RestoreDatabase()
        {
            this.Cursor = Cursors.WaitCursor;
            SqlConnection connection = new SqlConnection();
            try
            {
                connection.ConnectionString = "Data Source=" + LVYModule.ReturnIfNull(this.CboTenServer.Text, "", modGeneral.TypeOfValue.Nothing).ToString().Trim() + ";Initial Catalog=master;User Id=" + this.TxtUserName.Text + ";Password=" + this.TxtPass.Text + ";";
                connection.Open();
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                LVYModule.ShowWarning("Th\x00f4ng tin đăng nhập server kh\x00f4ng đ\x00fang. \rVui l\x00f2ng kiểm tra lại Username hoặc Password!");
                this.Cursor = Cursors.Default;
                ProjectData.ClearProjectError();
                return;
            }
            try
            {
                string str;
                if (this.pLoai == 0)
                {
                    str = "RESTORE DATABASE " + this.TxtTenCSDL.Text + " FROM DISK = '" + Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\DATA\DRAGON2_NEW'  WITH MOVE 'ERP2007_Data' TO '" + this.txtThuMuc.Text.Trim() + @"\" + this.TxtTenCSDL.Text.Trim() + ".mdf',   MOVE 'ERP2007_Log' TO '" + this.txtThuMuc.Text.Trim() + @"\" + this.TxtTenCSDL.Text.Trim() + "_Log.ldf' ";
                }
                else
                {
                    str = "RESTORE DATABASE " + this.TxtTenCSDL.Text + " FROM DISK = '" + Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\DATA\DRAGON2_MAU'  WITH MOVE 'ERP2007_Data' TO '" + this.txtThuMuc.Text.Trim() + @"\" + this.TxtTenCSDL.Text.Trim() + ".mdf',   MOVE 'ERP2007_Log' TO '" + this.txtThuMuc.Text.Trim() + @"\" + this.TxtTenCSDL.Text.Trim() + "_log.ldf' ";
                }
                new SqlCommand(str, connection).ExecuteNonQuery();
                connection.Close();
                this.Cursor = Cursors.Default;
                MessageBox.Show("Khởi tạo cơ sở dữ liệu th\x00e0nh c\x00f4ng !", "Thong Bao", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                LVYModule.SaveConnectionStringToXML(this.CboTenServer.Text, this.TxtTenCSDL.Text, this.TxtUserName.Text, this.TxtPass.Text);
                LVYModule.InitConnection();
                this.Dispose();
            }
            catch (Exception exception3)
            {
                ProjectData.SetProjectError(exception3);
                Exception exception2 = exception3;
                LVYModule.ShowWarning("C\x00f3 lỗi xảy ra! Vui l\x00f2ng chọn thư mục lưu trữ kh\x00e1c!");
                this.Cursor = Cursors.Default;
                ProjectData.ClearProjectError();
                return;
                ProjectData.ClearProjectError();
            }
            this.Cursor = Cursors.Default;
        }

        internal virtual ComboBox CboTenServer
        {
            [DebuggerNonUserCode]
            get
            {
                return this._CboTenServer;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                this._CboTenServer = value;
            }
        }

        internal virtual Button CmdThoat
        {
            [DebuggerNonUserCode]
            get
            {
                return this._CmdThoat;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                EventHandler handler = new EventHandler(this.CmdThoat_Click);
                if (this._CmdThoat != null)
                {
                    this._CmdThoat.Click -= handler;
                }
                this._CmdThoat = value;
                if (this._CmdThoat != null)
                {
                    this._CmdThoat.Click += handler;
                }
            }
        }

        internal virtual Button CmdThucHien
        {
            [DebuggerNonUserCode]
            get
            {
                return this._CmdThucHien;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                EventHandler handler = new EventHandler(this.CmdThucHien_Click);
                if (this._CmdThucHien != null)
                {
                    this._CmdThucHien.Click -= handler;
                }
                this._CmdThucHien = value;
                if (this._CmdThucHien != null)
                {
                    this._CmdThucHien.Click += handler;
                }
            }
        }

        internal virtual Button cmdTim
        {
            [DebuggerNonUserCode]
            get
            {
                return this._cmdTim;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                EventHandler handler = new EventHandler(this.cmdTim_Click);
                if (this._cmdTim != null)
                {
                    this._cmdTim.Click -= handler;
                }
                this._cmdTim = value;
                if (this._cmdTim != null)
                {
                    this._cmdTim.Click += handler;
                }
            }
        }

        internal virtual FolderBrowserDialog FolderBrowserDialog1
        {
            [DebuggerNonUserCode]
            get
            {
                return this._FolderBrowserDialog1;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                this._FolderBrowserDialog1 = value;
            }
        }

        internal virtual GroupBox GroupBox1
        {
            [DebuggerNonUserCode]
            get
            {
                return this._GroupBox1;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                this._GroupBox1 = value;
            }
        }

        internal virtual Label Label1
        {
            [DebuggerNonUserCode]
            get
            {
                return this._Label1;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                this._Label1 = value;
            }
        }

        internal virtual Label Label2
        {
            [DebuggerNonUserCode]
            get
            {
                return this._Label2;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                this._Label2 = value;
            }
        }

        internal virtual Label Label3
        {
            [DebuggerNonUserCode]
            get
            {
                return this._Label3;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                this._Label3 = value;
            }
        }

        internal virtual Label Label4
        {
            [DebuggerNonUserCode]
            get
            {
                return this._Label4;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                this._Label4 = value;
            }
        }

        internal virtual Label Label5
        {
            [DebuggerNonUserCode]
            get
            {
                return this._Label5;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                this._Label5 = value;
            }
        }

        internal virtual TextBox TxtPass
        {
            [DebuggerNonUserCode]
            get
            {
                return this._TxtPass;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                this._TxtPass = value;
            }
        }

        internal virtual TextBox TxtTenCSDL
        {
            [DebuggerNonUserCode]
            get
            {
                return this._TxtTenCSDL;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                this._TxtTenCSDL = value;
            }
        }

        internal virtual TextBox txtThuMuc
        {
            [DebuggerNonUserCode]
            get
            {
                return this._txtThuMuc;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                this._txtThuMuc = value;
            }
        }

        internal virtual TextBox TxtUserName
        {
            [DebuggerNonUserCode]
            get
            {
                return this._TxtUserName;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                this._TxtUserName = value;
            }
        }
    }
}

